<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;

class UserSync extends Model
{
    use SoftDeletes;
    protected $table = 'users_sync';
    public $appends = [
        // 'ubid'
    ];

    public function getObjAttribute()
    {
        $v = [];
        if (!empty($this->datas)) {
            if (is_string($this->datas)) {
                $v = json_decode($this->datas);
            }
        }
        return (object) $v;
    }

    /*
     * 获取关联用户ID
     * 此ID与关联用户实例ID不一定相等
     * 由于用户删除后，实例可能为空*/
    public function getUbidAttribute()
    {
        $union = $this->union();
        return @$union->user_id;
    }

    /*
     * 获取关联用户实例*/
    public function user()
    {
        if (empty(@$this->unique_id)) {
            return new Admin();
        }
        $admin = Admin::withTrashed()->where(['unique_id' => @$this->unique_id])->first();
        if (empty($admin->id)) {
            return new Admin();
        }
        return $admin;
        // return Admin::findOrNew(@$union->user_id);

        /*$union = $this->union();
        return Admin::findOrNew(@$union->user_id);*/
        // return $this->belongsToOne('App\Models\Admin', 'users_union', 'user_id', 'union_id');
    }

    public function union()
    {
        return DB::table('users_union')->where('union_id', $this->union_id)->first();
    }

    /*
     * 更新关联ID*/
    public function update_userid($user_id)
    {
        DB::table('users_union')->where('union_id', $this->union_id)->delete();
        return DB::table('users_union')->insert([
            'union_id' => $this->union_id,
            'user_id' => $user_id
        ]);
    }

    /*
     * 删除关联ID*/
    public function delete_userid()
    {
        return DB::table('users_union')->where('union_id', $this->union_id)->delete();
    }

    /*
     * 数据入库
     * 加入用户已被删除，则无法入库*/
    public function dispatch()
    {
        if (empty(@$this->unique_id)) {
            return fs2('用户名为空不能处理！');
        }
        /*$union = $this->union();
        $admin = Admin::findOrNew(@$union->user_id);*/
        $admin = Admin::withTrashed()->where(['unique_id' => @$this->unique_id])->first();
        if (empty($admin->id)) {
            $admin = new Admin();
        }
        $obj = $this->obj;
        if ($admin->trashed()) {
            $admin->restore();
            // return fs2('已删除用户不允许更新');
        }
        /*if ($admin->id != @$union->user_id) {
            return fs2('已删除用户不允许更新');
            // return fs403('已删除用户不允许更新！');
        }*/
        /*省代*/
        if ($this->user_type != $admin->user_type) {

        }
        if ($this->user_type == 2) {
            $l = [
                // 'A' => 'id',
                'B' => 'unique_id',
                // 'C' => 'password',
                'D' => 'abbr',
                'E' => 'company_name',
                'F' => 'extension',
                'G' => 'phone_number',
                'H' => 'email_address',
                'I' => 'status',
                'J' => 'region_id',
                'K' => 'province_id',
                'L' => 'city',
                'M' => 'address',
                'N' => 'first_name',
                'O' => 'cell_phone',
                'P' => 'wechat_id',
                'Q' => 'note',
            ];
            /*if ($obj->unique_id) {
                $c = DB::table('users')->where('unique_id', $obj->unique_id)->where('id', '!=', $admin->id)->count();
            } else {
                if (empty($admin->unique_id)) {
                    return fs2('用户名不能为空！');
                }
            }*/
            /*$password = $obj->password ?: $admin->password;
            if (empty($password)) {
                return fs2('密码不能为空！');
            }*/
            foreach ($l as $k => $v) {
                $admin->$v = $obj->$v ?: $admin->$v;
            }
            $admin->user_type = 2;
            // $admin->password = bcrypt($admin->password);
            $admin->save();
            $this->update_userid($admin->id);
            return fs1('更新成功！');
            /*if (empty($c)) {
                foreach ($l as $k => $v) {
                    $admin->$v = $obj->$v;
                }
                $admin->user_type = 2;
                // $admin->password = bcrypt($admin->password);
                $admin->save();
                $this->update_userid($admin->id);
                return fs1('更新成功！');
            } else {
                return fs2('系统中存在相同账号的用户');
            }*/
        } elseif ($this->user_type == 3) {
            $l = [
                'A' => 'unique_id',
                'B' => 'mdtype',
                'C' => 'creator_id',
                'D' => 'abbr',
                'E' => 'company_name',
                'F' => 'extension',
                'G' => 'phone_number',
                'H' => 'email_address',
                'I' => 'status',
                'J' => 'region_id',
                'K' => 'province_id',
                'L' => 'city',
                'M' => 'address',
                'N' => 'first_name',
                'O' => 'cell_phone',
                'P' => 'wechat_id',
                'Q' => 'note',
            ];
            /*if ($obj->unique_id) {
                $c = DB::table('users')->where('unique_id', $obj->unique_id)->where('id', '!=', $admin->id)->count();
            } else {
                if (empty($admin->unique_id)) {
                    return fs2('用户名不能为空！');
                }
            }
            $password = $obj->password ?: $admin->password;
            if (empty($password)) {
                return fs2('密码不能为空！');
            }*/
            foreach ($l as $k => $v) {
                $admin->$v = $obj->$v ?: $admin->$v;
                $admin->$v = $admin->$v ?: '';
            }
            $admin->user_type = $admin->user_type ?: 3;
            // $admin->password = bcrypt($admin->password);
            $admin->save();
            $this->update_userid($admin->id);
            return fs1('更新成功！');
        }
    }


}
